import cv2
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))

img1=cv2.imread('hat.png',cv2.IMREAD_GRAYSCALE)
cv2.imshow('1',img1)

img2=cv2.morphologyEx(img1,cv2.MORPH_ERODE,kernel)
cv2.imshow('2',img2)

img3=cv2.morphologyEx(img1,cv2.MORPH_DILATE,kernel)
cv2.imshow('3',img3)

img4=cv2.morphologyEx(img1,cv2.MORPH_OPEN,kernel)
cv2.imshow('4',img4)

img5=cv2.morphologyEx(img1,cv2.MORPH_CLOSE,kernel)
cv2.imshow('5',img5)

img6=cv2.morphologyEx(img1,cv2.MORPH_GRADIENT,kernel)
cv2.imshow('6',img6)

img7=cv2.morphologyEx(img1,cv2.MORPH_TOPHAT,kernel)
cv2.imshow('7',img7)

img8=cv2.morphologyEx(img1,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow('8',img8)

cv2.waitKey()
cv2.destroyAllWindows()